<?php

namespace app\admin\model;

use think\Db;
use think\db\Query;
use think\Model;

class User extends Model
{

    // 查询所有用户
    public function alluser()
    {
        // 查询用户信息,得到一个集合
        $list = Db('user')->field('pwd',true)->select();
        // 遍历用户信息查询 角色 
        foreach ($list as $v ) { 
            // 查询用户角色表,以userid为条件
            $rids[] = Db('user_role')->field('rid')->where('uid',$v['id'])->select();
        }
        foreach ($rids as $ky =>  $value) {
            foreach ($value as $keys => $va) {
            $rids[$ky][$keys] = Db('role')->field('name')->where('id',$va['rid'])->find();
            }
        }
        foreach ($rids as $key => $val ) {
            // var_dump($val,$key);
            $list[$key]['rids'] = $val;
        }
        return $list;
    }

    //获取user用户基本信息表数据
    public function getUser($id = '')
    {
        $query = new Query();
        $list = $query->table('gsq_user user,gsq_user_role roles,gsq_role role')
            ->where("user.id=roles.uid and roles.rid = role.id and user.id=".$id)
            ->field('user.id as id,user.name as username,user.tel as tel,role.name as roles')
            ->select();
        return $list;
    }

}
